<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单校验</title>
    <script>
        function tipUsername() {
            document.getElementById("s1").innerHTML = "<span style='font-size: 12px; color: #96a7ff;border: 1px solid black;'>用户名由字母、数字、下划线、点、减号组成,只能以数字字母开头和结尾,长度4-18位</span>"
        }
        function checkUsername(){
            let username = document.getElementById("username").value;
            let regExp = /^[a-z0-9][a-z0-9_\.-]{2,16}[a-z0-9]$/i;
            if(regExp.test(username)){
                document.getElementById("s1").innerHTML = "<span style='color:green;'>用户名格式正确</span>";
                // document.getElementById("btn").disabled = false;
                return true;
            }
            document.getElementById("s1").innerHTML = "<span style='color:red;'>您输入的用户名格式有误</span>";
            // document.getElementById("btn").disabled = true;
            return false;
        }

        function checkPhone(){
            let phone = document.getElementById("phone").value;
            let regExp = /^[1][3589]\d{9}$/;
            if(regExp.test(phone)){
                document.getElementById("s3").innerHTML = "<span style='color:green;'>手机号码格式正确</span>";
                // document.getElementById("btn").disabled = false;
                return true;
            }
            document.getElementById("s3").innerHTML = "<span style='color:red;'>您输入的手机号码格式有误</span>";
            // document.getElementById("btn").disabled = true;
            return false;
        }

        function checkForm() {
            if(checkUsername() && checkPhone()){
                return true;
            }
            return false;
        }
    </script>
</head>
<body>
<form action="29-正则表达式练习.html" onsubmit="return checkForm()">
<!--<form action="29-正则表达式练习.html">-->
    用户名:<input type="text" id="username" onfocus="tipUsername()" onblur="checkUsername()"><span id="s1"></span><br>
    密码:<input type="password" id="password"><br>
    电话:<input type="text" id="phone" onblur="checkPhone()"><span id="s3"></span><br>
    邮箱:<input type="text" id="email"><br>
    <input type="submit" id="btn" value="提交">
</form>
</body>
</html>